// 用于统一管理项目接口
// 登陆接口
import axios from 'axios'
const baseUrl = 'http://localhost:8888/api/private/v1/'
axios.defaults.baseURL = baseUrl
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  let token = localStorage.getItem('token')
  if (token) {
    config.headers['Authorization'] = token
  }
  return config
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error)
})
// 登陆验证接口
export const checkUser = (params) => {
  return axios.post('login', params).then(res => res.data)
}
// 获取菜单
export const getMenu = () => {
  return axios.get('menus').then(res => res.data)
}
// 获取用户列表
export const getUsers = (params) => {
  return axios.get('users', params).then(res => res.data)
}
// 添加用户
export const addUser = (params) => {
  return axios.post('users', params).then(res => res.data)
}
// 根据id查询用户
export const getUserById = (id) => {
  return axios.get(`users/${id}`).then(res => res.data)
}
// 编辑用户
export const editUser = (param) => {
  return axios.put(`users/${param.id}`, param).then(res => res.data)
}
// 删除用户
export const deleteUser = (id) => {
  return axios.delete(`users/${id}`).then(res => res.data)
}
// 根据id查询用户
export const changeUserState = (param) => {
  return axios.put(`users/${param.uId}/state/${param.type}`, param).then(res => res.data)
}
// 分配用户角色
export const grantUserRole = (param) => {
  return axios.put(`users/${param.id}/role`, param).then(res => res.data)
}
// 获取角色列表
export const getRoleList = () => {
  return axios.get(`roles`).then(res => res.data)
}
// 获取权限列表
export const getRightList = params => {
  return axios.get(`rights/${params.type}`).then(res => res.data)
}
// 添加角色
export const addRole = (params) => {
  return axios.post('roles', params).then(res => res.data)
}
// 根据id查询角色
export const getRoleById = (id) => {
  return axios.get(`roles/${id}`).then(res => res.data)
}
// 编辑角色
export const editRole = (param) => {
  return axios.put(`roles/${param.id}`, param).then(res => res.data)
}
// 删除角色
export const deleteRole = (id) => {
  return axios.delete(`roles/${id}`).then(res => res.data)
}
// 删除角色指定权限
export const deleteRoleRight = params => {
  return axios.delete(`roles/${params.roleId}/rights/${params.rightId}`).then(res => res.data)
}
// 角色授权
export const grantRoleRight = (roleId, rids) => {
  return axios.post(`roles/${roleId}/rights`, rids).then(res => res.data)
}
// 获取商品分类信息
export const getCategories = (params) => {
  return axios.get('categories', {params: params}).then(res => res.data)
}
// 添加分类
export const addCategories = params => {
  return axios.post('categories', params).then(res => res.data)
}
